#include <string.h>
#include <stdio.h>
#include <device.h>
#include <unistd.h>
#include <ctype.h>
#include <conio.h>

#include "Output.h"
#include "Directories.h"
#include "BbsControlData.h"
#include "UserPortRS232.h"
#include "Session.h"
#include "Twist.h"
#include "Input.h"

#define FILE_BUFFER_SIZE 127
char fileBuffer[FILE_BUFFER_SIZE + 1];

const char * getEncodingSuffix(encoding_t encoding)
{
	switch(encoding)
	{
	case EN_PETSCII: return "pet";
	case EN_ATASCII: return "atr";
	case EN_ANSI:	 return "ans";
	}

	return "txt";
}

// Outputs a string to the specified directions.
// If the output is to go to the modem, then the encoding is applied to that output stream.
bool outputString(encoding_t encoding, outDirections_t directions, const char *value)
{
	return outputStringML(_currentSession.sessionEncoding, encoding, directions, value);

	//static bool result;
	//static int i;
	//static char c, b, a;

	//result = true;

	//if(directions & OD_SCREEN) printf(value);
	//if(directions & OD_MODEM)
	//{
	//	setRTS(false);
	//	for(i = 0; i < strlen(value); ++i)
	//	{
	//		a = b = 0;
	//		if(directions & OD_MODEM)
	//		{
	//			b = tolower(sgetc());
	//		}
	//		if(directions & OD_SCREEN && kbhit())
	//		{
	//			a = tolower(cgetc());
	//		}

	//		if(a == 'a' || b == 'a')
	//		{
	//			//printf("aborted a: %c  b: %c", a, b);
	//			result = false;
	//			break;
	//		}

	//		if(a == ' ' || b == ' ')
	//		{
	//			pause(encoding, directions);
	//			if(_connectState != CS_CONNECTED) return false;
	//		}

	//		c = value[i];
	//		if(_currentSession.sessionEncoding == EN_ASCII) c = twistCharPtoA(c);
	//		if(c == CH_ENTER && _currentSession.sessionEncoding == EN_ASCII)
	//		{
	//			if(directions & OD_MODEM) writeByteRS232(10);
	//			//if(directions & OD_SCREEN) printf("%c", 10);
	//		}
	//		if(c > 0) 
	//		{
	//			if(directions & OD_MODEM) writeByteRS232(c);
	//			//if(directions & OD_SCREEN) printf("%c", c);
	//		}
	//	}
	//	setRTS(true);
	//}

	//return result;
}

void outputCharacter(encoding_t encoding, outDirections_t directions, char c)
{
	outputCharML(_currentSession.sessionEncoding, encoding, directions, c);

	//if(directions & OD_SCREEN) putc(c, stdout);
	//if(directions & OD_MODEM)
	//{
	//	if(_currentSession.sessionEncoding == EN_ASCII) 
	//	{
	//		c = twistCharPtoA(c);
	//		if(c == CH_ENTER)
	//		{
	//			writeByteRS232(10);
	//		}
	//	}

	//	if(c > 0) writeByteRS232(c);
	//}
}

// Outputs a file to the specified directions.
// If the output is to go to the modem, then the encoding is applied to that output stream.
int outputFile(encoding_t encoding, outDirections_t directions, const char *name, dirInfo_t *dir)
{
	static FILE *file;
	static char oldDirectory[17];
	static char directory[17];
	static char filename[21];
	static int result;
	static int c;

	//getcwd(oldDirectory, 17);
	getdevicedir(bcd.bbsDirectory.device, directory, 17);
	chdir(directory);

	result = 0;

	sprintf(filename, "%d:%s.%s", dir->drive, name, getEncodingSuffix(encoding));

	file = fopen(filename, "r");

	if(file != NULL)
	{
		result = 1;
		while((c = fread(fileBuffer, 1, FILE_BUFFER_SIZE, file)) == FILE_BUFFER_SIZE)
		{
			fileBuffer[c] = 0;
			if(!outputString(encoding, directions, fileBuffer)) break;
		}

		if(c > 0 && c < FILE_BUFFER_SIZE)
		{
			fileBuffer[c] = 0;
			outputString(encoding, directions, fileBuffer);
		}

	}
	else
	{
		outputString(encoding, directions, filename);
	}

	fclose(file);

	//chdir(oldDirectory);

	return result;
}
